package service

import (
	"anban/models"
	"anban/utils"
	"github.com/astaxie/beego/orm"
	"net/url"
	"strconv"
	"time"
)

// 获取学生信息
func GetStudentInfo(p map[string]interface{}) *models.Student {
	o := orm.NewOrm()
	student := &models.Student{}
	qs := o.QueryTable("Student")
	if _, ok := p["relation"]; ok && p["relation"].(bool) {
		qs = qs.RelatedSel()
	}
	if _, ok := p["id"]; ok {
		qs = qs.Filter("id", p["id"])
	}
	if _, ok := p["notId"]; ok {
		qs = qs.Exclude("id", p["notId"])
	}
	if _, ok := p["sn"]; ok {
		qs = qs.Filter("sn", p["sn"])
	}
	if _, ok := p["realName"]; ok {
		qs = qs.Filter("real_name", p["realName"])
	}
	if _, ok := p["idCard"]; ok {
		qs = qs.Filter("id_card", p["idCard"])
	}
	qs.One(student)
	if _, ok := p["convert"]; ok && p["convert"].(bool) {
		if student.Id > 0 {
			student.SexShow = models.StudentSex[student.Sex]
			student.StatusShow = models.StudentStatus[student.Status]
			student.CreatedShow = student.Created.Format("2006-01-02 15:04:05")
			student.UpdatedShow = student.Updated.Format("2006-01-02 15:04:05")
		}
	}
	if _, ok := p["api"]; ok && p["api"].(bool) {
		if student.Id > 0 {
			student.ClassName = student.Class.Name
		}
	}
	return student
}

// 添加学生信息
func AddStudent(input url.Values) (int64, error) {
	o := orm.NewOrm()
	school := &models.School{}
	if _, ok := input["school_id"]; ok {
		schoolId := utils.Atoi64(input["school_id"][0])
		school.Id = schoolId
		o.Read(school)
	}
	class := &models.Class{}
	if _, ok := input["class_id"]; ok {
		classId := utils.Atoi64(input["class_id"][0])
		class.Id = classId
		o.Read(class)
	}
	student := &models.Student{}
	student.School = school
	student.Class = class
	if _, ok := input["sn"]; ok {
		student.Sn = input["sn"][0]
	}
	if _, ok := input["real_name"]; ok {
		student.RealName = input["real_name"][0]
	}
	if _, ok := input["id_card"]; ok {
		student.IdCard = input["id_card"][0]
	}
	if _, ok := input["sex"]; ok {
		student.Sex, _ = strconv.Atoi(input["sex"][0])
	}
	if _, ok := input["status"]; ok {
		student.Status, _ = strconv.Atoi(input["status"][0])
	}
	student.TempIsNormal = 1
	return o.Insert(student)
}

// 修改学生信息
func EditStudent(input url.Values) (int64, error) {
	o := orm.NewOrm()
	p := orm.Params{}
	for k, v := range input {
		if k == "id" || k == "_xsrf" {
			continue
		}
		p[k] = v[0]
	}
	p["updated"] = time.Now().Format("2006-01-02 15:04:05")
	return o.QueryTable("Student").Filter("id", input["id"][0]).Update(p)
}

// 获取学生列表
func GetStudentList(p map[string]interface{}) (int64, []*models.Student) {
	var studentList []*models.Student
	o := orm.NewOrm()
	qs := o.QueryTable("Student")
	if _, ok := p["relation"]; ok && p["relation"].(bool) {
		qs = qs.RelatedSel()
	}
	if _, ok := p["userId"]; ok {
		qs = qs.Filter("Users__User__Id", p["userId"])
	}
	if _, ok := p["familySn"]; ok {
		qs = qs.Filter("family_sn", p["familySn"])
	}
	if _, ok := p["schoolId"]; ok {
		qs = qs.Filter("school_id", p["schoolId"])
	}
	if _, ok := p["schoolSn"]; ok {
		qs = qs.Filter("School__Sn", p["schoolSn"])
	}
	if _, ok := p["classId"]; ok {
		qs = qs.Filter("class_id", p["classId"])
	}
	if _, ok := p["classIds"]; ok && len(p["classIds"].([]int64)) > 0 {
		qs = qs.Filter("class_id__in", p["classIds"])
	}
	if _, ok := p["notIds"]; ok && len(p["notIds"].([]int64)) > 0 {
		qs = qs.Exclude("id__in", p["notIds"])
	}
	if _, ok := p["sn"]; ok {
		qs = qs.Filter("sn", p["sn"])
	}
	if _, ok := p["realName"]; ok {
		qs = qs.Filter("real_name__contains", p["realName"])
	}
	if _, ok := p["idCard"]; ok {
		qs = qs.Filter("id_card__contains", p["idCard"])
	}
	if _, ok := p["status"]; ok {
		qs = qs.Filter("status", p["status"])
	}
	if _, ok := p["travelId"]; ok {
		qs = qs.Filter("Travels__Travel__Id", p["travelId"])
	}
	totalCount, _ := qs.Count()
	curPage, ok := p["curPage"]
	perCount, _ := p["perCount"]
	if ok {
		start := perCount.(int) * (curPage.(int) - 1)
		qs = qs.Limit(perCount, start)
	}
	qs.OrderBy("-created").All(&studentList)
	if _, ok := p["convert"]; ok && p["convert"].(bool) {
		for _, student := range studentList {
			student.SexShow = models.StudentSex[student.Sex]
			student.StatusShow = models.StudentStatus[student.Status]
			student.CreatedShow = student.Created.Format("2006-01-02 15:04:05")
			student.UpdatedShow = student.Updated.Format("2006-01-02 15:04:05")
		}
	}
	if _, ok := p["api"]; ok && p["api"].(bool) {
		for _, student := range studentList {
			if student.Class.Id > 0 {
				student.ClassName = student.Class.Name
			}
			if _, ok := p["apiWithTeacher"]; ok && p["apiWithTeacher"].(bool) {
				param := map[string]interface{}{}
				param["api"] = true
				param["apiWithCourse"] = true
				param["teacherClassId"] = student.Class.Id
				_, student.TeacherList = GetUserList(param)
			}
		}
	}
	if _, ok := p["withTeacher"]; ok && p["withTeacher"].(bool) {
		param := map[string]interface{}{}
		param["withCourse"] = true
		for _, student := range studentList {
			param["teacherClassId"] = student.Class.Id
			_, student.TeacherList = GetUserList(param)
		}
	}
	return totalCount, studentList
}